<?php
Globals::requireController('Admin');
Globals::requireTable('Recommendseetype');
Globals::requireTable('Recommendsee');
Globals::requireTable('Recommendseedetail');
Globals::requireTable('Recommendseek');
Globals::requireTable('Config');

class RecommendseeController extends AdminController
{
    protected $RecommendseetypeTable;
    protected $RecommendseeTable;
    protected $RecommendseedetailTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->RecommendseetypeTable = new RecommendseetypeTable($config);
		$this->RecommendseeTable = new RecommendseeTable($config);
		$this->RecommendseedetailTable = new RecommendseedetailTable($config);
		$this->RecommendseekTable = new RecommendseekTable($config);
		$this->ConfigTable = new ConfigTable($config);
	}
	
	public function listAction(){
	    $seachname = $this->getParam('searchname');
	    $this->view->searchname = $seachname;
	    
	    $where = " 1 = 1 ";
	    
	    if(!empty($seachname)){
	        $where .= " and title like '%{$seachname}%' ";
	    }
	    
	    $count = $this->RecommendseeTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->RecommendseeTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    
	    $type_data = array();
	    $tmp_data = $this->RecommendseetypeTable->listAll();
	    foreach ($tmp_data as $v){
	        $type_data[$v['id']] = $v;
	    }
	    $this->view->type_data = $type_data;
	}
	
	public function editAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $title = $this->RecommendseeTable->getRow($id);
	        if($title['id'] > 0){
	            $this->view->data = $title;
	        }
	    }
	    
	    $type_data = array();
	    $tmp_data = $this->RecommendseetypeTable->listAll();
	    foreach ($tmp_data as $v){
	        $type_data[$v['id']] = $v;
	    }
	    $this->view->type_data = $type_data;
	}
	
	public function doeditAction()
	{
	    $id = $this->getIntParam('id');
	    $data['title'] = $this->getParam('title');
	    $data['type'] = $this->getParam('type');
	    $data['remark'] = $this->getParam('remark');
	    $data['icon'] = $this->getParam('icon');
	    if($id > 0){
	        $this->RecommendseeTable->update($data,$id);
	    }else{
	        $this->RecommendseeTable->add($data);
	    }
	    $this->redirectToAction('list');
	    exit();
	}
	
	public function deleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->RecommendseeTable->delete($id);
	    $this->redirectToAction('list');
	    exit();
	}
	
	public function setplaylistAction(){
	    $id = $this->getIntParam('id');
	    $where = array('rid' => $id);
	    $count = $this->RecommendseedetailTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->RecommendseedetailTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    
	    $this->view->rid = $id;
	}
	
	public function setplayeditAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $title = $this->RecommendseedetailTable->getRow($id);
	        if($title['id'] > 0){
	            $this->view->data = $title;
	        }
	    }
	    
	    $rid = $this->getIntParam('rid');
	    $this->view->rid = $rid;
	}
	
	public function setplaydoeditAction()
	{
	    $id = $this->getIntParam('id');
	    $data['rid'] = $this->getIntParam('rid');
	    $data['name'] = $this->getParam('name');
	    $data['remark'] = $this->getParam('remark');
	    $data['icon'] = $this->getParam('icon');
	    $data['playtypename'] = $this->getParam('playtypename');
	    $data['playurl'] = $this->getParam('playurl');
	    if($id > 0){
	        $this->RecommendseedetailTable->update($data,$id);
	    }else{
	        $this->RecommendseedetailTable->add($data);
	    }
	    $this->redirectToAction('setplaylist',array('id'=>$data['rid']));
	    exit();
	}
	
	public function setplaydeleteAction()
	{
	    $rid = $this->getIntParam('rid');
	    $id = $this->getIntParam('id');
	    $this->RecommendseedetailTable->delete($id);
	    $this->redirectToAction('setplaylist',array('id'=>$rid));
	    exit();
	}
	
	
	public function typelistAction()
	{
	    $where = array();
	    $count = $this->RecommendseetypeTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->RecommendseetypeTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    
	}
	
	public function edittypeAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $title = $this->RecommendseetypeTable->getRow($id);
	        if($title['id'] > 0){
	            $this->view->data = $title;
	        }
	    }
	    
	}
	
	public function doedittypeAction()
	{
	    $id = $this->getIntParam('id');
	    $data['title'] = $this->getParam('title');
	    if($id > 0){
	        $this->RecommendseetypeTable->update($data,$id);
	    }else{
            $this->RecommendseetypeTable->add($data);
	    }
	    $this->redirectToAction('typelist');
	    exit();
	}
	
	public function deletetypeAction()
	{
	    $id = $this->getIntParam('id');
	    $this->RecommendseetypeTable->delete($id);
	    $this->redirectToAction('typelist');
	    exit();
	}
	
	
	public function seeklistAction()
	{
	    $config_data = $this->ConfigTable->listAll("id in (5,6,7)");
	    foreach ($config_data as $v){
	        $config_data[$v['id']] = $v;
	    }
	    $this->view->config_data = $config_data;
	    
	    $where = array();
	    $count = $this->RecommendseekTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->RecommendseekTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    
	}
	
	public function doeditseekconfigAction(){
	    $data5 = $this->getParam('data5');
	    $data6 = $this->getParam('data6');
	    $data7 = $this->getParam('data7');
	    
	    $this->ConfigTable->update(array('value'=>date('Y-m-d H:i:s')),4);
	    $this->ConfigTable->update(array('value'=>$data5),5);
	    $this->ConfigTable->update(array('value'=>$data6),6);
	    $this->ConfigTable->update(array('value'=>$data7),7);
	    
	    $this->redirectToAction('seeklist');
	    exit();
	}
	
	public function editseekAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $title = $this->RecommendseekTable->getRow($id);
	        if($title['id'] > 0){
	            $this->view->data = $title;
	        }
	    }
	    
	}
	
	public function doeditseekAction()
	{
	    $id = $this->getIntParam('id');
	    $data['name'] = $this->getParam('name');
	    $data['url'] = $this->getParam('url');
	    if($id > 0){
	        $this->RecommendseekTable->update($data,$id);
	    }else{
	        $this->RecommendseekTable->add($data);
	    }
	    $this->ConfigTable->update(array('value'=>date('Y-m-d H:i:s')),4);
	    $this->redirectToAction('seeklist');
	    exit();
	}
	
	public function deleteseekAction()
	{
	    $id = $this->getIntParam('id');
	    $this->RecommendseekTable->delete($id);
	    $this->ConfigTable->update(array('value'=>date('Y-m-d H:i:s')),4);
	    $this->redirectToAction('seeklist');
	    exit();
	}
}
